package com.topisv.tms.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.topisv.tms.entity.CusStockMerge;

public interface CusStockMergeDao extends JpaSpecificationExecutor<CusStockMerge> , PagingAndSortingRepository<CusStockMerge, Long> {

	@Query("select c from CusStockMerge c where customerCode=? and warehouseCode=? and sku=?")
	CusStockMerge findCustomerWarSku(String customerCode,String warehouseCode,String sku);
	
	@Query("select c from CusStockMerge c where customerCode=? and warehouseCode=? and sku=?")
	List<CusStockMerge> findCustomerWarSku2(String customerCode,String warehouseCode,String sku);
	
	@Query("select c from CusStockMerge c where customerCode=? and sku=?")
	List<CusStockMerge> findCustomerWarSku(String customerCode,String sku);
	
	@Modifying
	@Query(nativeQuery=true, value="update cus_stock_merge set order_Num=(if(order_num<?4,0,order_num-?4)), version=version+1 where customer_code=?1 and warehouse_code=?2 and sku=?3 limit 1")
	int updateCusStockMerge(String customerCode, String warehouseCode, String sku, int num);
}
